package com.wgo.precise.client.ui.model;

import org.eclipse.core.runtime.IStatus;


import com.wgo.bpot.client.session.DomainSessionException;
import com.wgo.precise.client.ui.controller.ModelStatus;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.controller.exceptions.RematoClientException;

/**
 * @author petterei
 *
 * @version $Id: ClientPropertyValue.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public class ClientPropertyValue<T> extends PropertyValue {

	private T item = null;
    private ClientPropertyDefinition<T> definition = null;

    protected ClientPropertyValue(T item, ClientPropertyDefinition<T> definition) {
        if (definition == null) { // Assert
            throw new RematoClientException("Error when creating new value. " + definition.getClass().getSimpleName() + " not set.");
        } 
        this.definition = definition;
        if (item == null) { // Assert
            throw new RematoClientException("Error when creating new value. " + item.getClass().getSimpleName() + " not set.");
        } 
        this.item = item;
        
//        if (this.getValue() != null) {
//            try {
//            	definition.getPropertyType().getClazz().cast(this.getValue());
//            } catch (ClassCastException e) {
//                throw new RematoClientException(new ModelStatus(IStatus.ERROR, "Class-type-mismatch: " + this.getValue().getClass() + "(value) and " + definition.getPropertyType().getClazz()+ "(PropertyType)", null));
//            }
//        } else {
//            setDefaultValue();
//        }
    }

    @Override
	public AbstractPropertySpesification getPropertySpesification() {
        return definition;
    }
    
    
	/**
	 * @url element://model:project::client-model/design:view:::V21xx84ehfzdvplopzayh
	 */
	
    @Override
    public Object getValue() {
    	Object value = null;
    	try {
    		value = definition.getValue(item);
    	} catch (DomainSessionException e) {
    		RequirementPlugin.logAndDisplay(new ModelStatus(IStatus.ERROR, "Error in DomainSession. A value may not been retrieved.", e));
    	}
//        if (null == value) {
//        	value = definition.getPropertyType().createObjectValue(definition.getDefaultValue());
//        }
        return value;
    }

    @Override
    protected void setValue(Object value) {
    	try {
    		definition.setValue(item, value);
    	} catch (DomainSessionException e) {
    		RequirementPlugin.logAndDisplay(new ModelStatus(IStatus.ERROR, "Error in DomainSession. A value may not have been set.", e));
    	}
    }

//    @SuppressWarnings("unchecked")
//	@Override
//    protected void setPropertySpesification(AbstractPropertySpesification definition) {
//        Assert.notNull(definition);
//        if (definition instanceof ClientPropertyDefinition) {
//        	this.definition = (ClientPropertyDefinition)definition;
//        } else {
//            throw new RematoClientException("Error when setting PropertyDefinition. " + item.getClass().getSimpleName() + " not set.");
//        }
//    }
}
